package com.emuporium.gymrat.server.exercise;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.ExerciseDB;

@SuppressWarnings("serial")
public class ExerciseInfoUpdateSubmitServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String newName = request.getParameter("newName");
		Integer newMuscleGroup = new Integer(request.getParameter("newMuscleGroup"));
		int exerciseId = new Integer(request.getParameter("id"));
		String variationIds[] = request.getParameterValues("variation");
		ArrayList<Integer> varIds = new ArrayList<Integer>();
		String equipmentIds[] = request.getParameterValues("equipment");
		ArrayList<Integer> equipIds = new ArrayList<Integer>();

		if (variationIds != null)
			for (String s : variationIds) {
				varIds.add(new Integer(s));
			}
		if (equipmentIds != null)
			for (String s : equipmentIds) {
				equipIds.add(new Integer(s));
			}
		
		if(newName.equals("") || newMuscleGroup == null)
			ExerciseDB.updateExercise(exerciseId, null, null, varIds, equipIds);
		else
			ExerciseDB.updateExercise(exerciseId, newName, newMuscleGroup, varIds, equipIds);

		request.setAttribute("redirecturl", "/exercise/list");
		String url = "/includes/redirect.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}
}
